<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button v-if="$AUTH('finance:invoice:add')" type="primary" icon="el-icon-plus" @click="add"></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <!-- 日期时间范围 -->
          <el-date-picker v-model="search.createTime" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;"></el-date-picker>
          <el-input v-model="search.invoiceId" placeholder="开票信息" clearable></el-input>
          <el-input v-model="search.departmentId" placeholder="企业" clearable></el-input>
          <el-input v-model="search.type" placeholder="1支付发票 2收款发票 1对方开给我方的   2我方开给对方的" clearable></el-input>
          <el-input v-model="search.refType" placeholder="关联业务类型" clearable></el-input>
          <el-input v-model="search.refId" placeholder="关联业务id" clearable></el-input>
          <el-input v-model="search.buyerName" placeholder="企业名称--买方" clearable></el-input>
          <el-input v-model="search.buyerCode" placeholder="纳税人识别号" clearable></el-input>
          <el-input v-model="search.buyerTelephone" placeholder="电话" clearable></el-input>
          <el-input v-model="search.buyerAddress" placeholder="企业地址" clearable></el-input>
          <el-input v-model="search.buyerBankName" placeholder="开户银行的名称" clearable></el-input>
          <el-input v-model="search.buyerBankAccount" placeholder="银行账号" clearable></el-input>
          <el-input v-model="search.status" placeholder="状态1待生效  2待支付  3已支付" clearable></el-input>
          <el-input v-model="search.totalAmount" placeholder="总金额" clearable></el-input>
          <el-input v-model="search.taxRate" placeholder="税率%" clearable></el-input>
          <el-input v-model="search.sellerName" placeholder="企业名称--销售方" clearable></el-input>
          <el-input v-model="search.sellerCode" placeholder="纳税人识别号" clearable></el-input>
          <el-input v-model="search.sellerTelephone" placeholder="电话" clearable></el-input>
          <el-input v-model="search.sellerAddress" placeholder="企业地址" clearable></el-input>
          <el-input v-model="search.sellerBankName" placeholder="开户银行的名称" clearable></el-input>
          <el-input v-model="search.sellerBankAccount" placeholder="银行账号" clearable></el-input>
          <el-input v-model="search.createId" placeholder="开票人" clearable></el-input>
          <el-input v-model="search.createTime" placeholder="开票时间" clearable></el-input>
          <el-input v-model="search.url" placeholder="图片" clearable></el-input>
          <el-input v-model="search.msg" placeholder="说明" clearable></el-input>
          <el-input v-model="search.deleted" placeholder="" clearable></el-input>
          <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" row-key="invoiceId" @selection-change="selectionChange" stripe>
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="开票信息" prop="invoiceId" width="150"></el-table-column>
        <el-table-column label="企业" prop="departmentId" width="150"></el-table-column>
        <el-table-column label="1支付发票 2收款发票 1对方开给我方的   2我方开给对方的" prop="type" width="150"></el-table-column>
        <el-table-column label="关联业务类型" prop="refType" width="150"></el-table-column>
        <el-table-column label="关联业务id" prop="refId" width="150"></el-table-column>
        <el-table-column label="企业名称--买方" prop="buyerName" width="150"></el-table-column>
        <el-table-column label="纳税人识别号" prop="buyerCode" width="150"></el-table-column>
        <el-table-column label="电话" prop="buyerTelephone" width="150"></el-table-column>
        <el-table-column label="企业地址" prop="buyerAddress" width="150"></el-table-column>
        <el-table-column label="开户银行的名称" prop="buyerBankName" width="150"></el-table-column>
        <el-table-column label="银行账号" prop="buyerBankAccount" width="150"></el-table-column>
        <el-table-column label="状态1待生效  2待支付  3已支付" prop="status" width="150"></el-table-column>
        <el-table-column label="总金额" prop="totalAmount" width="150"></el-table-column>
        <el-table-column label="税率%" prop="taxRate" width="150"></el-table-column>
        <el-table-column label="企业名称--销售方" prop="sellerName" width="150"></el-table-column>
        <el-table-column label="纳税人识别号" prop="sellerCode" width="150"></el-table-column>
        <el-table-column label="电话" prop="sellerTelephone" width="150"></el-table-column>
        <el-table-column label="企业地址" prop="sellerAddress" width="150"></el-table-column>
        <el-table-column label="开户银行的名称" prop="sellerBankName" width="150"></el-table-column>
        <el-table-column label="银行账号" prop="sellerBankAccount" width="150"></el-table-column>
        <el-table-column label="开票人" prop="createId" width="150"></el-table-column>
        <el-table-column label="开票时间" prop="createTime" width="150"></el-table-column>
        <el-table-column label="图片" prop="url" width="150"></el-table-column>
        <el-table-column label="说明" prop="msg" width="150"></el-table-column>
        <el-table-column label="" prop="deleted" width="150"></el-table-column>
<!--    <el-table-column label="创建时间" prop="createTime" width="150" :formatter="formatDate"></el-table-column>-->
        <el-table-column label="操作" fixed="right" align="right">
          <template #default="scope">
            <el-button v-if="$AUTH('finance:invoice:edit')" type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button v-if="$AUTH('finance:invoice:del')" type="text" size="small" @click="table_del(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <invoiceEdit-dialog v-if="dialog.invoiceEdit" ref="invoiceEditDialog" @success="handleSaveSuccess" @closed="dialog.invoiceEdit=false"></invoiceEdit-dialog>
</template>

<script>
  import financeAPI from '@/api/finance/invoice';
  import invoiceEditDialog from './invoiceEdit';//1--导入
  export default {
    name: 'invoice',
    components: {
        invoiceEditDialog//2发布
    },
    data() {
      return {
        dialog: {
          invoiceEdit: false
        },
        apiObj: financeAPI.invoice.list,
        selection: [],
        search: {
          keyword: null,
          createTime: null
        }
      }
    },
    methods: {
      //添加
      add(){
        this.dialog.invoiceEdit = true
        this.$nextTick(() => {
          this.$refs.invoiceEditDialog.open()
        })
      },
      //编辑
      table_edit(row){
        this.dialog.invoiceEdit = true
        this.$nextTick(() => {
          this.$refs.invoiceEditDialog.open('edit').setData(row)
        })
      },
      //删除
      async table_del(row){
        var confirm = await this.$confirm('确定删除选中的项吗？', '提示', {
          confirmButtonText: 'ok',
          type: 'warning'
        }).catch(() => {})
        if(confirm !== 'confirm'){
          return false;
        }
        var res = await financeAPI.invoice.delete.delete(row.invoiceId);
        if(res.code === 2000){
          this.$refs.table.refresh();
          this.$message.success("删除成功");
        }else{
          this.$alert(res.message, "提示", {type: 'error'});
        }
      },
      //表格选择后回调事件
      selectionChange(selection){
        this.selection = selection;
      },
      //搜索
      upsearch(){
        this.$refs.table.upData(this.search);
      },
      //本地更新数据
      handleSaveSuccess(){
        this.$refs.table.refresh();
      },
      handlePermissionSuccess(){
        this.$refs.table.refresh();
      },
      handleDataSuccess(){
        this.$refs.table.refresh();
      },
      formatDate(row,column) {// 获取单元格数据
        let data = row[column.property];
        if(data == null) {
          return null;
        }
        let dt = new Date(data);
        return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
      },
    }
  }
</script>
<style>
</style>
